package handler.server;

import client.SwordHeartBeatClient;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

import java.nio.charset.StandardCharsets;

public class SwordHeartBeatHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		ByteBuf in = (ByteBuf) msg;
		byte[] bytes = new byte[in.readableBytes()];
		in.readBytes(bytes);
		String str = new String(bytes, StandardCharsets.UTF_8);
		if(str.equals(SwordHeartBeatClient.HEART_VALUE)){
			System.out.println("收到心跳回复");
			ReferenceCountUtil.release(msg);
		}else super.channelRead(ctx, msg);
	}
}
